package cn.iocoder.yudao.module.member.controller.admin.praxisProject;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.praxisProject.vo.*;
import cn.iocoder.yudao.module.member.service.praxisProject.PraxisProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 实践项目")
@RestController
@RequestMapping("/member/praxis-project")
@Validated
public class PraxisProjectController {

    @Resource
    private PraxisProjectService praxisProjectService;

    @PostMapping("/examinePage")
    @ApiOperation("待审核或审核项目列表分页")
    public CommonResult<PageResult<PraxisProjectRespVO>> examinePage(@Valid @RequestBody PraxisProjectPageReqVO pageVO) {
        pageVO.setQueryType(1);
        SortingField sortField = pageVO.getSortField();
        if(null==sortField){
            sortField=new SortingField();
            sortField.setOrder("desc");
            sortField.setField("t.weight");
            pageVO.setSortField(sortField);
        }
        PageResult<PraxisProjectRespVO> pageResult = praxisProjectService.getPraxisProjectPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @ApiOperation("获得实践项目")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, example = "1024", dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<PraxisProjectRespVO> getPraxisProject(@RequestParam("id") Long id) {
        PraxisProjectRespVO respVO = praxisProjectService.getProjectInfoById(id);
        respVO.setAnnexVOList(praxisProjectService.getPraxisAnnexListByProjectId(id,1));
        respVO.setScheduleVOList(praxisProjectService.getScheduleListByProjectId(id));
        return success(respVO);
    }

    @PutMapping("/examine")
    @ApiOperation("审核")
    @TenantIgnore
    public CommonResult<Boolean> examine(@Valid @RequestBody PraxisProjectExamineReqVO updateReqVO) {
        praxisProjectService.examinePraxisProject(updateReqVO);
        return success(true);
    }

    @GetMapping("/top")
    @ApiOperation("项目置顶")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "projectTypeId", value = "项目类型ID", required = true, dataTypeClass = Long.class)
    })
    @TenantIgnore
    public CommonResult<Boolean> top(@RequestParam("id") Long id,@RequestParam("projectTypeId") Long projectTypeId) {
        praxisProjectService.top(id,projectTypeId);
        return success(true);
    }

    @GetMapping("/getPraxisProjectList")
    @ApiOperation("项目列表集合(下拉框)")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "项目名称",    dataTypeClass = String.class),
            @ApiImplicitParam(name = "queryType", value = "查询类型",   dataTypeClass = Integer.class)
    })
    public CommonResult<List<PraxisProjectRespVO>> getPraxisProjectList( String title,Integer queryType) {
        List<PraxisProjectRespVO> pageResult = praxisProjectService.getPraxisProjectList(title,NumberUtils.getInteger(queryType,0));
        return success(pageResult);
    }

    @PostMapping("/saveProjectScheduleVO")
    @ApiOperation("保存赛程安排")
    @TenantIgnore
    public CommonResult<Boolean> saveProjectScheduleVO(@Valid @RequestBody PraxisProjectScheduleVO projectVO) {
        if(null==projectVO.getProjectId()){
            return error("项目ID不能为空");
        }
        praxisProjectService.saveProjectScheduleVO(projectVO);
        return success(true);
    }

    @PostMapping("/create")
    @ApiOperation("创建项目")
    public CommonResult<Long> createPraxisProject(@Valid @RequestBody PraxisProjectCreateReqVO projectVO) {
        Integer num = praxisProjectService.getProjectCountByTitle(projectVO.getTitle(), null);
        if(num>0){
            return error("该项目名称已存在,请重新输入");
        }
        List<PraxisProjectScheduleVO> scheduleVOList = projectVO.getScheduleVOList();
        String msg = praxisProjectService.validateScheduleTime(scheduleVOList,1);
        if(StringUtils.isNotBlank(msg)){
            return error(msg);
        }
        projectVO.setStatus(0);
        projectVO.setShowFlag(1);
        return success(praxisProjectService.createPraxisProject(projectVO));
    }

    @PostMapping("/update")
    @ApiOperation("编辑项目")
    @TenantIgnore
    public CommonResult<Long> updatePraxisProject(@Valid @RequestBody PraxisProjectUpdateReqVO projectVO) {
        if(null==projectVO.getId()){
            return error("ID不能为空");
        }
        Integer num = praxisProjectService.getProjectCountByTitle(projectVO.getTitle(), projectVO.getId());
        if(num>0){
            return error("该项目名称已存在,请重新输入");
        }
        List<PraxisProjectScheduleVO> scheduleVOList = projectVO.getScheduleVOList();
        String msg = praxisProjectService.validateScheduleTime(scheduleVOList,2);
        if(StringUtils.isNotBlank(msg)){
            return error(msg);
        }
        msg = praxisProjectService.validateUpdateSchedule(scheduleVOList,projectVO.getId());
        if(StringUtils.isNotBlank(msg)){
            return error(msg);
        }
        if(null==projectVO.getStatus()){
            projectVO.setExamineFlag("0");
            projectVO.setStatus(0);
        }
        praxisProjectService.updatePraxisProject(projectVO);
        return success(1L);
    }

    @GetMapping("/submitPraxisProject")
    @ApiOperation("发布")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Long> submitPraxisProject(@RequestParam("id") Long id) {
        praxisProjectService.submitPraxisProject(id);
        return success(1L);
    }

    @GetMapping("/underPraxisProject")
    @ApiOperation("下架")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Long> underPraxisProject(@RequestParam("id") Long id) {
        praxisProjectService.underPraxisProject(id);
        return success(1L);
    }


    @GetMapping("/updateShowFlag")
    @ApiOperation("隐藏/显示")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Long> updateShowFlag(@RequestParam("id") Long id) {
        praxisProjectService.updateShowFlag(id);
        return success(1L);
    }

    @PostMapping("/page")
    @ApiOperation("项目列表分页")
    @TenantIgnore
    public CommonResult<PageResult<PraxisProjectRespVO>> getPraxisProjectPage(@Valid @RequestBody PraxisProjectPageReqVO pageVO) {
        pageVO.setQueryType(2);
        PageResult<PraxisProjectRespVO> pageResult = praxisProjectService.getPraxisProjectPage(pageVO);
        return success(pageResult);
    }

    @PutMapping("/revert")
    @ApiOperation("还原项目")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> revertPraxisProject(@RequestParam("id") Long id) {
        praxisProjectService.revertPraxisProject(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除实践项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Boolean> deletePraxisProject(@RequestParam("id") Long id) {
        praxisProjectService.deletePraxisProject(id);
        return success(true);
    }

    @GetMapping("/getNextScheduleByProjectId")
    @ApiOperation("入选获得下一个阶段比赛信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "scheduleType", value = "类型;1:遴选;2:初赛;3:复赛;4:决赛;",required = true,   dataTypeClass = Integer.class)
    })
    @TenantIgnore
    public CommonResult<PraxisProjectScheduleVO> getNextScheduleByProjectId(@RequestParam("projectId") Long projectId, @RequestParam("scheduleType") Integer scheduleType) {
        return success(praxisProjectService.getNextScheduleByProjectId(projectId,scheduleType));
    }

    @PutMapping("/getRecordByProjectId")
    @ApiOperation("获取项目记录")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<List<PraxisProjectRecordVO>> getRecordByProjectId(@RequestParam("id") Long id) {
        List<PraxisProjectRecordVO> list = praxisProjectService.getRecordByProjectId(id);
        return success(list);
    }

    @PostMapping("/getFeedBackPageByProjectId")
    @ApiOperation("获取项目反馈")
    @TenantIgnore
    public CommonResult<PageResult<PraxisFeedBackVO>> getFeedBackPageByProjectId(@Valid @RequestBody PraxisFeedBackPageReqVO pageVO) {
        PageResult<PraxisFeedBackVO> pageResult = praxisProjectService.getFeedBackPageByProjectId(pageVO);
        return success(pageResult);
    }



    @GetMapping("/getPraxisProjectCount")
    @ApiOperation("实践项目统计")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<PraxisProjectCountVO> getPraxisProjectCount(@RequestParam("id") Long id) {
        PraxisProjectCountVO project = praxisProjectService.getPraxisProjectCount(id);
        return success(project);
    }
}
